//login.js
//获取应用实例
var utils = require('../../utils/util.js');
var app = getApp();
Page({
    data: {
        remind: '加载中',
        angle: 0,
        userInfo: {},
        regFlag: true

    },
    goToIndex: function () {
      wx.showLoading({
        title: '加载中',
      })
        wx.switchTab({
            url: '/pages/index/index',
        });
    },
    /*    tel:function () {
     wx.makePhoneCall({
     phoneNumber: app.globalData.phoneNumber,
     })
     },*/
    onLoad: function () {
        wx.setNavigationBarTitle({
            title: app.globalData.shopName
        });
        this.checkLogin();
    },
    onShow: function () {

    },

    // 核查是否分享过来的
    checkShare: function (token) {
        var that = this;
        var openId = utils.getCurrentPageUrlWithArgs().split('=')[1];
        if (openId === undefined) return;
        wx.request({
            url: app.buildUrl("/student/share"),
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                'token': token
            },
            data: {openId},
            method: 'POST',
            success: function (res) {
                var resp = res.data;
                if (resp.code != 200) {
                    app.alert({"content": resp.msg+'----分享'});
                    return;
                }
                that.setData({});
            }
        })
    },
    onReady: function () {
        var that = this;
        setTimeout(function () {
            that.setData({
                remind: ''
            });
        }, 1000);
        wx.onAccelerometerChange(function (res) {
            var angle = -(res.x * 30).toFixed(1);
            if (angle > 14) {
                angle = 14;
            }
            else if (angle < -14) {
                angle = -14;
            }
            if (that.data.angle !== angle) {
                that.setData({
                    angle: angle
                });
            }
        });
    },
    checkLogin: function () {
        var that = this;
        wx.login({
            success: function (res) {
                if (!res.code) {
                    app.alert({'content': '登录失败，请再次点击~~~'});
                    return;
                }
                wx.request({
                    url: app.buildUrl('/student/check-reg'),
                    method: 'POST',
                    header: app.getRequestHeader(),
                    data: {code: res.code},
                    success: function (res) {
                        if (res.data.code != 200) {
                            that.setData({
                                regFlag: false
                            });
                            return;
                        }
                        app.setCache("token", res.data.data.token);
                        app.globalData.openId = res.data.data.openId;
                        that.goToIndex();
                    }
                });
            }

        })

    },
    login: function (e) {
        var that = this;
        if (!e.detail.userInfo) {
            app.alert({'content': '登录失败，请再次点击~~~'});
            return;
        }
      wx.showLoading({
        title: '加载中',
      })
        var data = e.detail.userInfo;
        wx.login({
            success: function (res) {
                if (!res.code) {
                    app.alert({'content': '登录失败，请再次点击~~~'});
                    return;
                }
                data['code'] = res.code;
                wx.request({
                    url: app.buildUrl('/student/login'),
                    method: 'POST',
                    header: app.getRequestHeader(),
                    data: data,
                    success: function (res) {
                        if (res.data.code != 200) {
                            app.alert({'content': res.data.msg});
                            return;
                        }
                        app.globalData.openId = res.data.data.openId;
                        app.setCache("token", res.data.data.token);
                        that.checkShare(res.data.data.token);
                        wx.hideLoading()
                        that.goToIndex();
                    }
                });
            }
        })


    }
});